//
//  ViewController.m
//  Pagination
//
//  Created by XMac on 2018/10/30.
//  Copyright © 2018 XMac. All rights reserved.
//

#import "ViewController.h"
#import "CustomTableViewCell.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "extobjc.h"
#import "Pagination/Paging.h"
#import "JKFoundation.h"
#import "Pagination/DefaultPagingProcess.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *dataTableView;

@property (strong, nonatomic) AFHTTPSessionManager *manager;

@property (strong, nonatomic) Paging *pageControl;

@property (strong, nonatomic) NSMutableArray *data;

@end

@implementation ViewController



- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.data = @[].mutableCopy;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    
    [self configData];
    
    [self.dataTableView.mj_footer beginRefreshing];
}

-(void)configUI{
    [self.dataTableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:@"CustomTableViewCell"];
    @weakify(self);
    self.dataTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        [self requestData];
    }];
    [self.dataTableView.mj_footer setEndRefreshingCompletionBlock:^{
        @strongify(self);
        [self.dataTableView reloadData];
    }];
}


-(void)configData{
    _manager = [AFHTTPSessionManager manager];
    
    _manager.requestSerializer = [AFHTTPRequestSerializer new];
    _manager.responseSerializer = [AFJSONResponseSerializer new];
    
    
    DefaultPagingProcess *process = [DefaultPagingProcess new];
    @weakify(self);
    [process setNetworkStatusBlockWithSuccess:^(NSURLSessionDataTask * _Nonnull task, id _Nullable result) {
        @strongify(self);
        NSArray *data = [result jk_arrayForKey:@"data"];
        [self.data addObjectsFromArray:data];
    } withFailure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"network fail");
    } withUIFinishBlock:^{
        @strongify(self);
        [self.dataTableView.mj_footer endRefreshing];
    } withNoMore:^{
        @strongify(self);
        [self.dataTableView.mj_footer endRefreshingWithNoMoreData];
    }];
    
    _pageControl = [[Paging alloc]initWithDefaultPage:1 withDefaultSize:20 withProcess:process];
}

-(void)requestData{
    NSDictionary *params = @{
                             @"page":@(self.pageControl.nextPage),
                             @"size":@(self.pageControl.pageSize)
                             };
    
    [self.manager POST:@"http://localhost:8082/order/list" parameters:params progress:nil success:self.pageControl.getProcessSuccess failure:self.pageControl.getProcessFailure];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
    NSDictionary *data = self.data[indexPath.row];
    [cell setData:[data jk_stringForKey:@"id"] title:[data jk_stringForKey:@"name"] content:[data jk_stringForKey:@"total_price"]];
    return cell;
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}





@end
